package com.clkj.aimin.service.helper;

import com.clkj.aimin.dao.helper.HelperTalkMapper;
import com.clkj.aimin.dao.user.RechargeMapper;
import com.clkj.aimin.dao.user.UserMapper;
import com.clkj.aimin.dao.user.UserOrderImgMapper;
import com.clkj.aimin.dao.user.UserOrderMapper;
import com.clkj.aimin.pojo.common.DateFormatHelper;
import com.clkj.aimin.pojo.helper.HelperTalk;
import com.clkj.aimin.pojo.user.Recharge;
import com.clkj.aimin.pojo.user.User;
import com.clkj.aimin.pojo.user.UserOrder;
import com.clkj.aimin.pojo.user.UserOrderImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class HelperServiceImpl implements HelperService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HelperTalkMapper helperTalkMapper;
    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private UserOrderImgMapper userOrderImgMapper;

    @Override
    public  List<User> getPatientList(Integer userId) {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("helperId",userId);
        List<User> users = userMapper.selectByExample(example);
        return users;
    }




    @Override
    public User getHelperInfo(Integer helperId) {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("userId",helperId);
        return userMapper.selectOneByExample(example);
    }

    @Override
    public void addHelperTalk(Integer talkId, Integer userId, Integer helpId, String content) {
        HelperTalk helperTalk = new HelperTalk();
        helperTalk.setHelperId(helpId);
        helperTalk.setUserId(userId);
        helperTalk.setTalkDetail(content);
        helperTalk.setCreateTime(DateFormatHelper.currentTimes());
        helperTalk.setParentId(talkId);
        helperTalk.setUserType(1);
        helperTalk.setHelperIsNew(1);
        helperTalk.setUserIsNew(0);
        helperTalkMapper.insertSelective(helperTalk);
    }

    @Override
    public List<HelperTalk> getHelperTalkInfo(Integer helperId) {
      List<HelperTalk> helperTalkList =  helperTalkMapper.getHelperTalkInfo(helperId);
      helperTalkList.forEach(helperTalk -> {
          Example example = new Example(HelperTalk.class);
          Example.Criteria criteria = example.createCriteria();
          criteria.andEqualTo("userId",helperTalk.getUserId()).andEqualTo("helperIsNew",0);
          helperTalk.setHelperUnReadNum(helperTalkMapper.selectCountByExample(example));
      });
      return helperTalkList;
    }

    @Override
    public List<HelperTalk> getHelperTalk(Integer parentId) {
        Example example = new Example(HelperTalk.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("talkId",parentId);
        List<HelperTalk> helperTalks = helperTalkMapper.selectByExample(example);

        Example example2 = new Example(HelperTalk.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("parentId",parentId);
        List<HelperTalk> helperTalks2 = helperTalkMapper.selectByExample(example2);
        helperTalks.addAll(helperTalks2);
        helperTalks.forEach(helperTalk -> {
            Example example1 = new Example(User.class);
            example1.createCriteria().andEqualTo("userId",helperTalk.getUserId()).andEqualTo("helperId",helperTalk.getHelperId());
            HelperTalk helperTalk1= new HelperTalk();
            helperTalk1.setHelperIsNew(1);
            helperTalkMapper.updateByExampleSelective(helperTalk1,example1);
        });
        return helperTalks;
    }

    @Override
    public List<UserOrder> getOrderList(Integer userId,Integer flag) {
        List<UserOrder> list =   userOrderMapper.getOrderList(userId,flag);
        List<UserOrderImg> imgList= null;
        for (UserOrder order : list) {
            if(order.getOrderType()==1){
                Example example = new Example(UserOrderImg.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("orderNumber",order.getOrderNumber());
                //亲情就诊
                imgList =userOrderImgMapper.selectByExample(example);
                order.setImgList(imgList);

            }
        }

        return list;
    }

    @Override
    public void updateOrderStatus(Integer orderId,Integer status,String dateTime,String note) {
        UserOrder userOrder = new UserOrder();
        userOrder.setOrderId(orderId);
        if(status==0){
            //审核不通过
            userOrder.setOrderStatus(2);
            userOrder.setUnPass(note);
        }else{
            //审核通过
            userOrder.setOrderStatus(1);
            userOrder.setConfirmTime(DateFormatHelper.currentTimes());
            userOrder.setVisitTime(dateTime);

        }
        userOrderMapper.updateByPrimaryKeySelective(userOrder);
    }

    @Override
    public Map getPatientInfo(Integer userId) {
        //获取该用户基本信息
        User user = userMapper.selectByPrimaryKey(userId);
        //获取该用户的家庭成员
        List<User> users= new ArrayList<>();
        if(user.getParentCode()!=null){
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("parentCode",user.getParentCode());
            users = userMapper.selectByExample(example);
        }



        //历史缴费记录与申请记录
        Example example  = new Example(UserOrder.class);

        example.createCriteria().andEqualTo("userId",userId);
        example.orderBy("orderId").desc();
        List<UserOrder> orderList = userOrderMapper.selectByExample(example);


        Map map = new HashMap();
        map.put("user",user);
        map.put("users",users);
        map.put("userOrders",orderList);
        return map;
    }

}
